package game.util;

import org.apache.log4j.Logger;

import sun.io.ByteToCharConverter;


public class Unicode {
	private static final Logger logger = Logger.getLogger(Unicode.class);
	public static final String big5 = "Big5";
	public static final String gbk = "GBK";
	public static final String iso = "ISO-8859-1";
	public static final String utf = "UTF-8";

	public synchronized static String gbtou(String src) {
		StringBuffer sb = new StringBuffer();
		try {
			src = replace(src);
			ByteToCharConverter converter = ByteToCharConverter
					.getConverter(gbk);
			char[] c = converter.convertAll(src.getBytes());
			//char[] c = src.toCharArray();
			for (int i = 0; i < c.length; i++) {
				int xx = Integer.valueOf(c[i]);
				if (xx <= 4095)
					sb.append(c[i]);
				else
					sb.append("&#x" + Integer.toHexString(c[i]) + ";");
			}
		} catch (Exception e) {
			sb = new StringBuffer(src);
			logger.info("Unicode====gbtou==="+e.toString());
			//e.printStackTrace();
		}
		return sb.toString();
	}
    public synchronized static String replace(String src){
    	String value = src;
    	try{
    		//logger.info("value=xx=="+value);
    		for(;;){
	    		int begin = value.indexOf("<!--");
	    		if(begin<0)
	    			return value;
	    		int end = value.indexOf("-->");
	    		value = value.substring(0,begin)+value.substring(end+3,value.length());
	    		//logger.info("value=aa=="+value);
    		}
    	}catch(Exception e){
    		value = src;
    	}
    	return value;
    }
	
}
